package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;


import com.sgb.link.entity.merchant.InvestmentOptions;
import com.sgb.link.entity.merchant.InvestmentProjectEntity;
import com.sgb.link.entity.merchant.TradeCategoryEntity;
import com.sgb.link.utils.AddressHelper;
import com.sgb.merchant.api.MerchantsNet;

import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/6/17
 * Author:黄淋
 * Description:
 */
public class InvestmentProjectViewmodel extends BaseViewModel {
    public MutableLiveData<List<InvestmentProjectEntity>> liveDataInvestment = new MutableLiveData<>();
    public MutableLiveData<List<TradeCategoryEntity>> liveDataCategoryList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> liveDataCityList = new MutableLiveData<>();
    public MutableLiveData<Integer> ldState=new MutableLiveData<>();
    public MutableLiveData<InvestmentOptions> ldOptions=new MutableLiveData<>();

    private int pageNum = 1;

    public InvestmentProjectViewmodel(@NonNull Application application) {
        super(application);
    }

    public void getInvestmentData(boolean isLoadMore, Map<String, Object> map) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
        map.put("limit", 20);
        MerchantsNet.getInstance().getService().getInvestmentList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<InvestmentProjectEntity>>> data) {
                if(data.isSuccess()){
                    liveDataInvestment.postValue(data.getData().getList());
                }else{
                    liveDataInvestment.postValue(new ArrayList<InvestmentProjectEntity>());
                }

            }

            @Override
            public void onFail() {
                super.onFail();
                liveDataInvestment.postValue(new ArrayList<InvestmentProjectEntity>());

            }
        });

    }


    /**
     * 行业一级分类列表
     */
    public void getindustryFirstCategoryList() {
        MerchantsNet.getInstance().getService().getindustryFirstCategoryList().enqueue(new ICallBack<BaseEntity<List<TradeCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TradeCategoryEntity>> data) {
                if(data.isSuccess()) {
                    liveDataCategoryList.postValue(data.getData());
                }
            }
        });

    }

    /**
     * 获取服务城市
     */
    public void getServiceCity() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.CallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    liveDataCityList.postValue(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取项目状态
     * @param no
     */
    public void getState(Long no){
        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if(data.isSuccess()){
                    ldState.postValue(data.getData());
                }
            }
        });
    }

    public void getOptions(){
        MerchantsNet.getInstance().getService().getOptions().enqueue(new ICallBack<BaseEntity<InvestmentOptions>>() {
            @Override
            public void onSuccess(BaseEntity<InvestmentOptions> data) {
                if(data.isSuccess()){
                    ldOptions.postValue(data.getData());
                }
            }
        });
    }
}
